layui.use(['form','jquery','layer','laydate','upload'],function () {
    var form = layui.form;
    var $ = layui.$;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var upload = layui.upload;
    //注册时间渲染
    laydate.render({
        elem: '#registerDate',
        type: 'datetime'
    })
    //出生日期渲染
    laydate.render({
        elem: '#birthDate',
        type: 'datetime'
    })
    getDepts();
    //关闭按钮
    $('#close').click(function () {
        var layer = parent.layer;
        layer.close(layer.index);
    })
    //编辑按钮
    $('#compile').click(function () {
        //修改用户信息
        $.ajax({
            url: '/login/updateUserMessage',
            type: 'post',
            data: form.val('userForm'),
            dataType: 'json',
            success: function (data) {
                var currentUser = JSON.parse(localStorage.getItem("user"));
                var user = form.val('userForm');
                if(currentUser.data.userId == user.userId){
                    if(user.acatarUrl != ''){
                        parent.parent.$('#imgSrc').attr('src',user.acatarUrl);
                    }
                }
                var layer = parent.layer;
                layer.close(layer.index);
            }
        })
    });
    //修改头像按钮
    upload.render({
        elem: '#acatarUrl',
        url: '/login/uploadUserAcatar',
        accept: 'images',
        field: 'file',
        exts: 'png|jpg|gif',
        done: function(res, index, upload){
            if(res.code == 0){
                layer.msg('上传成功');
                $('#acatarUrl').attr('src',res.data.data.src);
                $('#acatarUrlVal').val(res.data.data.src);
            }
        }
    });
    //查询所有部门
    function getDepts() {
        $.ajax({
            url: '/SyDept/getDepts',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                var option = data.data;
                var deptId = $('#dept').val();
                for (var i = 0; i < option.length; i++) {
                    if(option[i].id != 0){
                        $('#deptId').append('<option value="' + option[i].id + '">' + option[i].deptName + '</option>');
                    }
                }
                $('#deptId').val(deptId);
                form.render();
            }
        });
    }
})